setwd("~/Dropbox/Project_fundraising/dataverse/")

#### Read-in ####
rm(list=ls())
library(tidyverse)
library(knitr)
library(stargazer)
library(sandwich)


df <- readRDS("Modified Data/Clean.RDS")
#### Additional Cleaning ####
df <- df %>% filter(!is.na(fund_amount_num) & !is.na(run_interest) & Level != "state")
df<- df %>% mutate(interest_cont = ifelse(run_interest == "Actively considering",1,NA),
                 interest_cont = ifelse(run_interest == "Open to possibility",0.5,interest_cont),
                 interest_cont = ifelse(run_interest == "No interest",0,interest_cont),
                 Potential_salary = total_comp,
                 Potential_fundraising = average_fund,
                 Past_fundraising = ifelse(fund_amount_num>0,1,0),
                 interest_cont2 = ifelse(run_interest != "No interest", 1, 0))

## tab_interest_Past_fund.tex ####
reg1 <- lm(interest_cont2 ~ Past_fundraising,d = df)
reg2 <- lm(interest_cont2 ~ Past_fundraising + state_ab + Type,d = df)
reg3 <- lm(interest_cont2 ~ Past_fundraising + state_ab + Type + log(Population),d = df)
reg4 <- lm(interest_cont2 ~ Past_fundraising + state_ab + Type + log(Population) + PID_3 + Gender + college + age,d = df)
#reg3 <- lm(interest_cont ~ Past_fundraising + log(Potential_salary) + log(Potential_fundraising),d = df)
#reg4 <- lm(interest_cont ~ Past_fundraising + log(Potential_salary) + log(Potential_fundraising) + Type + log(Population),d = df)

regs <- list(reg1,reg2,reg3,reg4)


vcov_1 <- vcovHC(reg1, "HC1")
vcov_2 <- vcovHC(reg2, "HC1")
vcov_3 <- vcovHC(reg3, "HC1")
vcov_4 <- vcovHC(reg4, "HC1")

se_1 <- sqrt(diag(vcov_1))
se_2 <- sqrt(diag(vcov_2))
se_3 <- sqrt(diag(vcov_3))
se_4 <- sqrt(diag(vcov_4))

ses <- list(se_1,se_2,se_3,se_4)

stargazer(regs, 
          out = "Docs/tables/tab_interest_past_fund.tex", 
          type= "latex",
          title="",header=T, float = F,
         keep = c("Constant","Population","fundraising"),
          column.sep.width = "30pt",
          model.numbers = F, omit.stat=c("ser","f","Rsq","adj.rsq"),
         add.lines = list(c("Position-level fixed effects","\\text{No}","\\text{Yes}","\\text{Yes}","\\text{Yes}"),
                          c("State fixed effects","\\text{No}","\\text{Yes}","\\text{Yes}","\\text{Yes}"),
         c("Respondent-level controls","\\text{No}","\\text{No}","\\text{No}","\\text{Yes}")),
         covariate.labels = c("Past fundraising", "Log constituency pop.", "Constant"),
          se=ses,digits = 2,  dep.var.caption = "", dep.var.labels.include = F, no.space = T,
         style = "ajps", align=TRUE,
          # notes = c("$^{*}$p$<$0.1; $^{**}$p$<$0.05; $^{***}$p$<$0.01"),
          notes.append = F, notes.align = "l",notes.label = "")


